package com.luofan.service.impl;

import com.luofan.common.result.ResultCodeEnum;
import com.luofan.common.util.UserContext;
import com.luofan.entity.dto.CollectUserDTO;
import com.luofan.entity.po.Archive;
import com.luofan.entity.po.Collect;
import com.luofan.entity.po.CollectUser;
import com.luofan.entity.vo.CollectUserVO;
import com.luofan.entity.vo.CollectVO;
import com.luofan.entity.vo.TeacherCollectVO;
import com.luofan.mapper.ArchiveMapper;
import com.luofan.mapper.CollectMapper;
import com.luofan.mapper.TermMapper;
import com.luofan.mapper.UserMapper;
import com.luofan.service.CollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luofan.service.CollectUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 档案收集任务表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-26
 */
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private TermMapper termMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollectUserService collectUserService;
    @Autowired
    private ArchiveMapper archiveMapper;

    //教研室主任 发布档案收集任务
    @Override
    @Transactional  //开启事务
    public ResultCodeEnum saveCollect(Collect collect) {
        //设置发布者id
        collect.setPublisherId(UserContext.getCurrentUserId());
        //获取当前时间为发布时间
        LocalDateTime now = LocalDateTime.now();
        collect.setPublishTime(now);
        //设置主任所在的教研室id
        Long currentSectionId = UserContext.getCurrentSectionId();
        collect.setSectionId(currentSectionId);

        //根据当前时间获取当前学期id
        Long termId = termMapper.getTermIdByTime(now).getId();
        collect.setTermId(termId);
        //如果没有获取到当前学期id，说明目前处于假期，返回提示信息
        if (termId == null) return ResultCodeEnum.HOLIDAY_WARN;

        //创建档案收集任务
        int rows = collectMapper.saveCollect(collect);
        if (rows == 0) return ResultCodeEnum.FAIL;

        //获取当前教研室所有教师id
        List<Long> userIdList = userMapper.listIdBySectionId(currentSectionId);
        //为对应教研室的每个教师创建一条待完成任务
        List<CollectUser> list = new ArrayList<>();
        for (Long userId : userIdList) {
            list.add(new CollectUser().setCollectId(collect.getId()).setUserId(userId));
        }
        //使用MP批量添加
        collectUserService.saveBatch(list, 100);

        return ResultCodeEnum.SUCCESS;
    }

    //根据教研室id和学期id获取所有对应的档案收集任务
    @Override
    public List<CollectVO> listCollectBySectionIdAndTermId(Long sectionId, Long termId) {
        return collectMapper.listCollectBySectionIdAndTermId(sectionId, termId);
    }

    //根据档案收集任务id获取其档案收集任务提交情况
    @Override
    public CollectUserVO listCollectCompletion(Long collectId) {
        //根据collectId获取collect_user信息
        List<CollectUser> collectUserList = collectMapper.listCollectCompletion(collectId);

        CollectUserVO collectUserVO = new CollectUserVO();
        //获取还未提交档案的用户名称封装到VO
        for (CollectUser collectUser : collectUserList) {
            if (collectUser.getIsSubmit() == 0) {
                //根据用户id获取用户名称
                String userName = userMapper.getUserNameById(collectUser.getUserId());
                CollectUserDTO collectUserDTO = new CollectUserDTO();
                collectUserDTO.setUserName(userName);
                collectUserDTO.setUserId(collectUser.getUserId());
                //封装用户名称到VO
                collectUserVO.getNotSubmitted().add(collectUserDTO);
            }
        }

        //根据不同情况封装已提交档案的用户名称封装到VO
        for (CollectUser collectUser : collectUserList) {
            if (collectUser.getIsSubmit() == 1) {
                //根据用户id和档案收集任务id获取档案信息(状态,提交时间)
                Archive archive = archiveMapper.getArchiveByUserIdAndCollectId(collectUser.getUserId(), collectId);

                CollectUserDTO collectUserDTO = new CollectUserDTO();
                //获取审核未通过的档案信息封装到VO
                if (archive.getStatus() == 3) {
                    collectUserDTO.setUserId(collectUser.getUserId());
                    collectUserDTO.setUserName(userMapper.getUserNameById(collectUser.getUserId()));
                    collectUserDTO.setUploadTime(archive.getUploadTime());
                    collectUserVO.getNotPassed().add(collectUserDTO);
                }
                //获取审核通过的档案信息封装到VO
                if (archive.getStatus() == 2) {
                    collectUserDTO.setUserId(collectUser.getUserId());
                    collectUserDTO.setUserName(userMapper.getUserNameById(collectUser.getUserId()));
                    collectUserDTO.setUploadTime(archive.getUploadTime());
                    collectUserVO.getPassed().add(collectUserDTO);
                }
                //获取待审核的档案信息封装到VO
                if (archive.getStatus() == 1) {
                    collectUserDTO.setUserId(collectUser.getUserId());
                    collectUserDTO.setUserName(userMapper.getUserNameById(collectUser.getUserId()));
                    collectUserDTO.setUploadTime(archive.getUploadTime());
                    collectUserVO.getToBeAudited().add(collectUserDTO);
                }
            }
        }

        return collectUserVO;
    }

    //根据教师id和学期id获取收到的档案收集任务
    @Override
    public List<TeacherCollectVO> listCollectByUserId(Long userId, Long termId) {
        List<TeacherCollectVO> teacherCollectVOList = new ArrayList<>();
        //先根据教师id获取collect_user表信息
        List<CollectUser> collectUserList = collectMapper.listByUserId(userId);

        for (CollectUser collectUser : collectUserList) {
            TeacherCollectVO teacherCollectVO = new TeacherCollectVO();
            //根据收集任务id和学期id获取剩余信息
            teacherCollectVO = collectMapper.getCollectByIdAndTermId(collectUser.getCollectId(), termId, collectUser.getUserId());
            //如果还未提交档案设置VO的完成情况为"未完成"
            if (collectUser.getIsSubmit() == 0) teacherCollectVO.setCompletionStatus(4);
            teacherCollectVOList.add(teacherCollectVO);
        }

        return teacherCollectVOList;
    }

}
